<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  JSON.stringify() 的高级用法
  参数1：将要序列化成一个json字符串的值
  参数2：函数，每一项会经过该函数的处理
  参数3：指定缩进的空白字符串，用于美化输出
  <script>
    function replacer(key, value) {
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }

    var foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7
    };
    var jsonString = JSON.stringify(foo, replacer);
    // value为undefined不会转换
    console.log(jsonString); // {"week":45,"month":7}

    // 第二个参数会深度遍历 
    var json = {
      name: "zhangsan",
      age: 30,
      lists: [
        { name: "xxx1", age: 11},
        { name: "xxx2", age: 12},
        { name: "xxx3", age: 13},
        { lists: { name: 'xxx4', age: 15}}
      ]
    };
    console.log(JSON.stringify(json, replacer));
    
    // 第二个参数也可以是数组 只打印数组中的元素值
    console.log(JSON.stringify(foo, ['week', 'month']));

    // 第三个参数用来指定分隔符  如果数数字表示用几个空格分隔
    console.log(JSON.stringify(json, null, 4));


  </script>
</body>

</html>